--DO NOT MODIFY THIS FILE. IT IS ALWAYS OVERWRITTEN ON GENERATION.
--Data Schema For Version 0.0.0.0

--CREATE TABLE [Category]
if not exists(select * from sysobjects where name = 'Category' and xtype = 'U')
CREATE TABLE [dbo].[Category] (
[CategoryId] [Int] IDENTITY (1, 1) NOT NULL ,
[Description] [Text] NULL ,
[Name] [VarChar] (50) NOT NULL ,
[Picture] [Image] NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__CATEGORY_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__CATEGORY_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [Customer]
if not exists(select * from sysobjects where name = 'Customer' and xtype = 'U')
CREATE TABLE [dbo].[Customer] (
[Code] [VarChar] (10) NOT NULL ,
[CompanyName] [VarChar] (50) NOT NULL ,
[Title] [VarChar] (50) NULL ,
[UserId] [Int] NOT NULL ,
[Email] [VarChar] (50) NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__CUSTOMER_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__CUSTOMER_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [Employee]
if not exists(select * from sysobjects where name = 'Employee' and xtype = 'U')
CREATE TABLE [dbo].[Employee] (
[BirthDate] [DateTime] NULL ,
[HireDate] [DateTime] NULL ,
[Notes] [Text] NULL ,
[PhoneExt] [VarChar] (4) NULL ,
[Photo] [Image] NULL ,
[PhotoPath] [VarChar] (200) NULL ,
[ReportsTo] [Int] NULL ,
[Title] [VarChar] (50) NULL ,
[TitleOfCourtesy] [VarChar] (50) NULL ,
[UserId] [Int] NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__EMPLOYEE_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__EMPLOYEE_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [EmployeeTerritory]
if not exists(select * from sysobjects where name = 'EmployeeTerritory' and xtype = 'U')
CREATE TABLE [dbo].[EmployeeTerritory] (
[TerritoryId] [VarChar] (20) NOT NULL ,
[UserId] [Int] NOT NULL )

GO

--CREATE TABLE [Order]
if not exists(select * from sysobjects where name = 'Order' and xtype = 'U')
CREATE TABLE [dbo].[Order] (
[EmployeeId] [Int] NULL ,
[Freight] [Money] NULL CONSTRAINT [DF__ORDER_FREIGHT] DEFAULT (0),
[OrderDate] [DateTime] NULL ,
[OrderId] [Int] IDENTITY (1, 1) NOT NULL ,
[RequiredDate] [DateTime] NULL ,
[ShipAddress] [VarChar] (100) NULL ,
[ShipCity] [VarChar] (50) NULL ,
[ShipCountry] [VarChar] (50) NULL ,
[ShipName] [VarChar] (50) NULL ,
[ShipPostalCode] [VarChar] (50) NULL ,
[ShipRegion] [VarChar] (50) NULL ,
[ShippedDate] [DateTime] NULL ,
[ShipperId] [Int] NULL ,
[UserId] [Int] NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__ORDER_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__ORDER_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [OrderDetail]
if not exists(select * from sysobjects where name = 'OrderDetail' and xtype = 'U')
CREATE TABLE [dbo].[OrderDetail] (
[Discount] [Money] NOT NULL CONSTRAINT [DF__ORDERDETAIL_DISCOUNT] DEFAULT (0),
[OrderDetailId] [Int] IDENTITY (1, 1) NOT NULL ,
[OrderId] [Int] NOT NULL ,
[ProductId] [Int] NOT NULL ,
[Quantity] [Int] NOT NULL CONSTRAINT [DF__ORDERDETAIL_QUANTITY] DEFAULT (1),
[UnitPrice] [Money] NOT NULL CONSTRAINT [DF__ORDERDETAIL_UNITPRICE] DEFAULT (0),
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__ORDERDETAIL_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__ORDERDETAIL_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [Product]
if not exists(select * from sysobjects where name = 'Product' and xtype = 'U')
CREATE TABLE [dbo].[Product] (
[CategoryId] [Int] NULL ,
[Discontinued] [Bit] NOT NULL CONSTRAINT [DF__PRODUCT_DISCONTINUED] DEFAULT (0),
[Name] [VarChar] (50) NOT NULL ,
[ProductId] [Int] IDENTITY (1, 1) NOT NULL ,
[QuantityPerUnit] [VarChar] (20) NULL ,
[ReorderLevel] [Int] NULL CONSTRAINT [DF__PRODUCT_REORDERLEVEL] DEFAULT (0),
[SupplierId] [Int] NULL ,
[UnitPrice] [Money] NULL CONSTRAINT [DF__PRODUCT_UNITPRICE] DEFAULT (0),
[UnitsInStock] [Int] NULL CONSTRAINT [DF__PRODUCT_UNITSINSTOCK] DEFAULT (0),
[UnitsOnOrder] [Int] NULL CONSTRAINT [DF__PRODUCT_UNITSONORDER] DEFAULT (0),
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__PRODUCT_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__PRODUCT_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [Region]
if not exists(select * from sysobjects where name = 'Region' and xtype = 'U')
CREATE TABLE [dbo].[Region] (
[Name] [VarChar] (50) NOT NULL ,
[RegionId] [Int] IDENTITY (1, 1) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__REGION_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__REGION_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [Shipper]
if not exists(select * from sysobjects where name = 'Shipper' and xtype = 'U')
CREATE TABLE [dbo].[Shipper] (
[CompanyName] [VarChar] (40) NOT NULL ,
[Phone] [VarChar] (24) NULL ,
[ShipperId] [Int] IDENTITY (1, 1) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__SHIPPER_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__SHIPPER_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [Supplier]
if not exists(select * from sysobjects where name = 'Supplier' and xtype = 'U')
CREATE TABLE [dbo].[Supplier] (
[CompanyName] [VarChar] (40) NOT NULL ,
[ContactTitle] [VarChar] (30) NULL ,
[HomePage] [VarChar] (100) NULL ,
[UserId] [Int] NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__SUPPLIER_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__SUPPLIER_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [SystemUser]
if not exists(select * from sysobjects where name = 'SystemUser' and xtype = 'U')
CREATE TABLE [dbo].[SystemUser] (
[Address] [VarChar] (50) NULL ,
[City] [VarChar] (50) NULL ,
[Country] [VarChar] (50) NULL ,
[Fax] [VarChar] (50) NULL ,
[FirstName] [VarChar] (50) NOT NULL ,
[LastName] [VarChar] (50) NOT NULL ,
[MiddleName] [VarChar] (50) NULL ,
[Phone] [VarChar] (50) NULL ,
[PostalCode] [VarChar] (50) NULL ,
[Region] [VarChar] (50) NULL ,
[UserId] [Int] IDENTITY (1, 1) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__SYSTEMUSER_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__SYSTEMUSER_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--CREATE TABLE [Territory]
if not exists(select * from sysobjects where name = 'Territory' and xtype = 'U')
CREATE TABLE [dbo].[Territory] (
[Name] [VarChar] (50) NOT NULL ,
[RegionId] [Int] NOT NULL ,
[TerritoryId] [VarChar] (20) NOT NULL ,
[ModifiedBy] [Varchar] (50) NULL,
[ModifiedDate] [DateTime] CONSTRAINT [DF__TERRITORY_MODIFIEDDATE] DEFAULT getdate() NULL,
[CreatedBy] [Varchar] (50) NULL,
[CreatedDate] [DateTime] CONSTRAINT [DF__TERRITORY_CREATEDDATE] DEFAULT getdate() NULL,
[Timestamp] [timestamp] NOT NULL
)

GO

--##SECTION BEGIN [FIELD CREATE]
--TABLE [Category] ADD FIELDS
if exists(select * from sys.objects where name = 'Category' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CategoryId' and o.name = 'Category')
ALTER TABLE [dbo].[Category] ADD [CategoryId] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'Category' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Description' and o.name = 'Category')
ALTER TABLE [dbo].[Category] ADD [Description] [Text] NULL 
if exists(select * from sys.objects where name = 'Category' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Name' and o.name = 'Category')
ALTER TABLE [dbo].[Category] ADD [Name] [VarChar] (50) NOT NULL 
if exists(select * from sys.objects where name = 'Category' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Picture' and o.name = 'Category')
ALTER TABLE [dbo].[Category] ADD [Picture] [Image] NULL 
GO
--TABLE [Customer] ADD FIELDS
if exists(select * from sys.objects where name = 'Customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Code' and o.name = 'Customer')
ALTER TABLE [dbo].[Customer] ADD [Code] [VarChar] (10) NOT NULL 
if exists(select * from sys.objects where name = 'Customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CompanyName' and o.name = 'Customer')
ALTER TABLE [dbo].[Customer] ADD [CompanyName] [VarChar] (50) NOT NULL 
if exists(select * from sys.objects where name = 'Customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Title' and o.name = 'Customer')
ALTER TABLE [dbo].[Customer] ADD [Title] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'Customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UserId' and o.name = 'Customer')
ALTER TABLE [dbo].[Customer] ADD [UserId] [Int] NOT NULL 
if exists(select * from sys.objects where name = 'Customer' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Email' and o.name = 'Customer')
ALTER TABLE [dbo].[Customer] ADD [Email] [VarChar] (50) NULL 
GO
--TABLE [Employee] ADD FIELDS
if exists(select * from sys.objects where name = 'Employee' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'BirthDate' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [BirthDate] [DateTime] NULL 
if exists(select * from sys.objects where name = 'Employee' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'HireDate' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [HireDate] [DateTime] NULL 
if exists(select * from sys.objects where name = 'Employee' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Notes' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [Notes] [Text] NULL 
if exists(select * from sys.objects where name = 'Employee' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'PhoneExt' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [PhoneExt] [VarChar] (4) NULL 
if exists(select * from sys.objects where name = 'Employee' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Photo' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [Photo] [Image] NULL 
if exists(select * from sys.objects where name = 'Employee' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'PhotoPath' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [PhotoPath] [VarChar] (200) NULL 
if exists(select * from sys.objects where name = 'Employee' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ReportsTo' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [ReportsTo] [Int] NULL 
if exists(select * from sys.objects where name = 'Employee' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Title' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [Title] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'Employee' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TitleOfCourtesy' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [TitleOfCourtesy] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'Employee' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UserId' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [UserId] [Int] NOT NULL 
GO
--TABLE [EmployeeTerritory] ADD FIELDS
if exists(select * from sys.objects where name = 'EmployeeTerritory' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TerritoryId' and o.name = 'EmployeeTerritory')
ALTER TABLE [dbo].[EmployeeTerritory] ADD [TerritoryId] [VarChar] (20) NOT NULL 
if exists(select * from sys.objects where name = 'EmployeeTerritory' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UserId' and o.name = 'EmployeeTerritory')
ALTER TABLE [dbo].[EmployeeTerritory] ADD [UserId] [Int] NOT NULL 
GO
--TABLE [Order] ADD FIELDS
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'EmployeeId' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [EmployeeId] [Int] NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Freight' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [Freight] [Money] NULL CONSTRAINT [DF__ORDER_FREIGHT] DEFAULT (0)
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'OrderDate' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [OrderDate] [DateTime] NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'OrderId' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [OrderId] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'RequiredDate' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [RequiredDate] [DateTime] NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ShipAddress' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [ShipAddress] [VarChar] (100) NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ShipCity' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [ShipCity] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ShipCountry' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [ShipCountry] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ShipName' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [ShipName] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ShipPostalCode' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [ShipPostalCode] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ShipRegion' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [ShipRegion] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ShippedDate' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [ShippedDate] [DateTime] NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ShipperId' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [ShipperId] [Int] NULL 
if exists(select * from sys.objects where name = 'Order' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UserId' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [UserId] [Int] NOT NULL 
GO
--TABLE [OrderDetail] ADD FIELDS
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Discount' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [Discount] [Money] NOT NULL CONSTRAINT [DF__ORDERDETAIL_DISCOUNT] DEFAULT (0)
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'OrderDetailId' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [OrderDetailId] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'OrderId' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [OrderId] [Int] NOT NULL 
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ProductId' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [ProductId] [Int] NOT NULL 
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Quantity' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [Quantity] [Int] NOT NULL CONSTRAINT [DF__ORDERDETAIL_QUANTITY] DEFAULT (1)
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UnitPrice' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [UnitPrice] [Money] NOT NULL CONSTRAINT [DF__ORDERDETAIL_UNITPRICE] DEFAULT (0)
GO
--TABLE [Product] ADD FIELDS
if exists(select * from sys.objects where name = 'Product' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CategoryId' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [CategoryId] [Int] NULL 
if exists(select * from sys.objects where name = 'Product' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Discontinued' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [Discontinued] [Bit] NOT NULL CONSTRAINT [DF__PRODUCT_DISCONTINUED] DEFAULT (0)
if exists(select * from sys.objects where name = 'Product' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Name' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [Name] [VarChar] (50) NOT NULL 
if exists(select * from sys.objects where name = 'Product' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ProductId' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [ProductId] [Int] IDENTITY (1, 1) NOT NULL 
if exists(select * from sys.objects where name = 'Product' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'QuantityPerUnit' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [QuantityPerUnit] [VarChar] (20) NULL 
if exists(select * from sys.objects where name = 'Product' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ReorderLevel' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [ReorderLevel] [Int] NULL CONSTRAINT [DF__PRODUCT_REORDERLEVEL] DEFAULT (0)
if exists(select * from sys.objects where name = 'Product' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'SupplierId' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [SupplierId] [Int] NULL 
if exists(select * from sys.objects where name = 'Product' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UnitPrice' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [UnitPrice] [Money] NULL CONSTRAINT [DF__PRODUCT_UNITPRICE] DEFAULT (0)
if exists(select * from sys.objects where name = 'Product' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UnitsInStock' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [UnitsInStock] [Int] NULL CONSTRAINT [DF__PRODUCT_UNITSINSTOCK] DEFAULT (0)
if exists(select * from sys.objects where name = 'Product' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UnitsOnOrder' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [UnitsOnOrder] [Int] NULL CONSTRAINT [DF__PRODUCT_UNITSONORDER] DEFAULT (0)
GO
--TABLE [Region] ADD FIELDS
if exists(select * from sys.objects where name = 'Region' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Name' and o.name = 'Region')
ALTER TABLE [dbo].[Region] ADD [Name] [VarChar] (50) NOT NULL 
if exists(select * from sys.objects where name = 'Region' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'RegionId' and o.name = 'Region')
ALTER TABLE [dbo].[Region] ADD [RegionId] [Int] IDENTITY (1, 1) NOT NULL 
GO
--TABLE [Shipper] ADD FIELDS
if exists(select * from sys.objects where name = 'Shipper' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CompanyName' and o.name = 'Shipper')
ALTER TABLE [dbo].[Shipper] ADD [CompanyName] [VarChar] (40) NOT NULL 
if exists(select * from sys.objects where name = 'Shipper' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Phone' and o.name = 'Shipper')
ALTER TABLE [dbo].[Shipper] ADD [Phone] [VarChar] (24) NULL 
if exists(select * from sys.objects where name = 'Shipper' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ShipperId' and o.name = 'Shipper')
ALTER TABLE [dbo].[Shipper] ADD [ShipperId] [Int] IDENTITY (1, 1) NOT NULL 
GO
--TABLE [Supplier] ADD FIELDS
if exists(select * from sys.objects where name = 'Supplier' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CompanyName' and o.name = 'Supplier')
ALTER TABLE [dbo].[Supplier] ADD [CompanyName] [VarChar] (40) NOT NULL 
if exists(select * from sys.objects where name = 'Supplier' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ContactTitle' and o.name = 'Supplier')
ALTER TABLE [dbo].[Supplier] ADD [ContactTitle] [VarChar] (30) NULL 
if exists(select * from sys.objects where name = 'Supplier' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'HomePage' and o.name = 'Supplier')
ALTER TABLE [dbo].[Supplier] ADD [HomePage] [VarChar] (100) NULL 
if exists(select * from sys.objects where name = 'Supplier' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UserId' and o.name = 'Supplier')
ALTER TABLE [dbo].[Supplier] ADD [UserId] [Int] NOT NULL 
GO
--TABLE [SystemUser] ADD FIELDS
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Address' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [Address] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'City' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [City] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Country' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [Country] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Fax' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [Fax] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'FirstName' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [FirstName] [VarChar] (50) NOT NULL 
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'LastName' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [LastName] [VarChar] (50) NOT NULL 
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'MiddleName' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [MiddleName] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Phone' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [Phone] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'PostalCode' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [PostalCode] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Region' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [Region] [VarChar] (50) NULL 
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'UserId' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [UserId] [Int] IDENTITY (1, 1) NOT NULL 
GO
--TABLE [Territory] ADD FIELDS
if exists(select * from sys.objects where name = 'Territory' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Name' and o.name = 'Territory')
ALTER TABLE [dbo].[Territory] ADD [Name] [VarChar] (50) NOT NULL 
if exists(select * from sys.objects where name = 'Territory' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'RegionId' and o.name = 'Territory')
ALTER TABLE [dbo].[Territory] ADD [RegionId] [Int] NOT NULL 
if exists(select * from sys.objects where name = 'Territory' and type = 'U') AND not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'TerritoryId' and o.name = 'Territory')
ALTER TABLE [dbo].[Territory] ADD [TerritoryId] [VarChar] (20) NOT NULL 
GO
--##SECTION END [FIELD CREATE]

--##SECTION BEGIN [AUDIT TRAIL CREATE]

--APPEND AUDIT TRAIL CREATE FOR TABLE [Category]
if exists(select * from sys.objects where name = 'Category' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Category')
ALTER TABLE [dbo].[Category] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Category' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Category')
ALTER TABLE [dbo].[Category] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__CATEGORY_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Category]
if exists(select * from sys.objects where name = 'Category' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Category')
ALTER TABLE [dbo].[Category] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Category' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Category')
ALTER TABLE [dbo].[Category] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__CATEGORY_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Category]
if exists(select * from sys.objects where name = 'Category' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Category')
ALTER TABLE [dbo].[Category] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Customer]
if exists(select * from sys.objects where name = 'Customer' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Customer')
ALTER TABLE [dbo].[Customer] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Customer' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Customer')
ALTER TABLE [dbo].[Customer] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__CUSTOMER_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Customer]
if exists(select * from sys.objects where name = 'Customer' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Customer')
ALTER TABLE [dbo].[Customer] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Customer' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Customer')
ALTER TABLE [dbo].[Customer] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__CUSTOMER_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Customer]
if exists(select * from sys.objects where name = 'Customer' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Customer')
ALTER TABLE [dbo].[Customer] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Employee]
if exists(select * from sys.objects where name = 'Employee' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Employee' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__EMPLOYEE_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Employee]
if exists(select * from sys.objects where name = 'Employee' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Employee' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__EMPLOYEE_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Employee]
if exists(select * from sys.objects where name = 'Employee' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Employee')
ALTER TABLE [dbo].[Employee] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Order]
if exists(select * from sys.objects where name = 'Order' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Order' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__ORDER_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Order]
if exists(select * from sys.objects where name = 'Order' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Order' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__ORDER_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Order]
if exists(select * from sys.objects where name = 'Order' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Order')
ALTER TABLE [dbo].[Order] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [OrderDetail]
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__ORDERDETAIL_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [OrderDetail]
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__ORDERDETAIL_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [OrderDetail]
if exists(select * from sys.objects where name = 'OrderDetail' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'OrderDetail')
ALTER TABLE [dbo].[OrderDetail] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Product]
if exists(select * from sys.objects where name = 'Product' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Product' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__PRODUCT_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Product]
if exists(select * from sys.objects where name = 'Product' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Product' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__PRODUCT_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Product]
if exists(select * from sys.objects where name = 'Product' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Product')
ALTER TABLE [dbo].[Product] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Region]
if exists(select * from sys.objects where name = 'Region' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Region')
ALTER TABLE [dbo].[Region] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Region' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Region')
ALTER TABLE [dbo].[Region] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__REGION_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Region]
if exists(select * from sys.objects where name = 'Region' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Region')
ALTER TABLE [dbo].[Region] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Region' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Region')
ALTER TABLE [dbo].[Region] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__REGION_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Region]
if exists(select * from sys.objects where name = 'Region' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Region')
ALTER TABLE [dbo].[Region] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Shipper]
if exists(select * from sys.objects where name = 'Shipper' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Shipper')
ALTER TABLE [dbo].[Shipper] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Shipper' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Shipper')
ALTER TABLE [dbo].[Shipper] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__SHIPPER_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Shipper]
if exists(select * from sys.objects where name = 'Shipper' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Shipper')
ALTER TABLE [dbo].[Shipper] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Shipper' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Shipper')
ALTER TABLE [dbo].[Shipper] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__SHIPPER_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Shipper]
if exists(select * from sys.objects where name = 'Shipper' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Shipper')
ALTER TABLE [dbo].[Shipper] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Supplier]
if exists(select * from sys.objects where name = 'Supplier' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Supplier')
ALTER TABLE [dbo].[Supplier] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Supplier' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Supplier')
ALTER TABLE [dbo].[Supplier] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__SUPPLIER_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Supplier]
if exists(select * from sys.objects where name = 'Supplier' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Supplier')
ALTER TABLE [dbo].[Supplier] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Supplier' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Supplier')
ALTER TABLE [dbo].[Supplier] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__SUPPLIER_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Supplier]
if exists(select * from sys.objects where name = 'Supplier' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Supplier')
ALTER TABLE [dbo].[Supplier] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [SystemUser]
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__SYSTEMUSER_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [SystemUser]
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__SYSTEMUSER_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [SystemUser]
if exists(select * from sys.objects where name = 'SystemUser' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'SystemUser')
ALTER TABLE [dbo].[SystemUser] ADD [Timestamp] [timestamp] NOT NULL

GO

--APPEND AUDIT TRAIL CREATE FOR TABLE [Territory]
if exists(select * from sys.objects where name = 'Territory' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'Territory')
ALTER TABLE [dbo].[Territory] ADD [CreatedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Territory' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'Territory')
ALTER TABLE [dbo].[Territory] ADD [CreatedDate] [DateTime] CONSTRAINT [DF__TERRITORY_CREATEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL MODIFY FOR TABLE [Territory]
if exists(select * from sys.objects where name = 'Territory' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'Territory')
ALTER TABLE [dbo].[Territory] ADD [ModifiedBy] [Varchar] (50) NULL
if exists(select * from sys.objects where name = 'Territory' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'Territory')
ALTER TABLE [dbo].[Territory] ADD [ModifiedDate] [DateTime] CONSTRAINT [DF__TERRITORY_MODIFIEDDATE] DEFAULT getdate() NULL

--APPEND AUDIT TRAIL TIMESTAMP FOR TABLE [Territory]
if exists(select * from sys.objects where name = 'Territory' and type = 'U') and not exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'Territory')
ALTER TABLE [dbo].[Territory] ADD [Timestamp] [timestamp] NOT NULL

GO

--##SECTION END [AUDIT TRAIL CREATE]

--##SECTION BEGIN [AUDIT TRAIL REMOVE]

--REMOVE AUDIT TRAIL CREATE FOR TABLE [EmployeeTerritory]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedBy' and o.name = 'EmployeeTerritory')
ALTER TABLE [dbo].[EmployeeTerritory] DROP COLUMN [CreatedBy]
if exists (select * from sys.objects where name = 'DF__EMPLOYEETERRITORY_CREATEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[EmployeeTerritory] DROP CONSTRAINT [DF__EMPLOYEETERRITORY_CREATEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'CreatedDate' and o.name = 'EmployeeTerritory')
ALTER TABLE [dbo].[EmployeeTerritory] DROP COLUMN [CreatedDate]

--REMOVE AUDIT TRAIL MODIFY FOR TABLE [EmployeeTerritory]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedBy' and o.name = 'EmployeeTerritory')
ALTER TABLE [dbo].[EmployeeTerritory] DROP COLUMN [ModifiedBy]
if exists (select * from sys.objects where name = 'DF__EMPLOYEETERRITORY_MODIFIEDDATE' and [type] = 'D')
ALTER TABLE [dbo].[EmployeeTerritory] DROP CONSTRAINT [DF__EMPLOYEETERRITORY_MODIFIEDDATE]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'ModifiedDate' and o.name = 'EmployeeTerritory')
ALTER TABLE [dbo].[EmployeeTerritory] DROP COLUMN [ModifiedDate]

--REMOVE AUDIT TRAIL TIMESTAMP FOR TABLE [EmployeeTerritory]
if exists (select * from syscolumns c inner join sysobjects o on c.id = o.id where c.name = 'Timestamp' and o.name = 'EmployeeTerritory')
ALTER TABLE [dbo].[EmployeeTerritory] DROP COLUMN [Timestamp]

GO

--##SECTION END [AUDIT TRAIL REMOVE]

--##SECTION BEGIN [RENAME PK]

--RENAME EXISTING PRIMARY KEYS IF NECESSARY
DECLARE @pkfixCategory varchar(500)
SET @pkfixCategory = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Category')
if @pkfixCategory <> '' and (BINARY_CHECKSUM(@pkfixCategory) <> BINARY_CHECKSUM('PK_CATEGORY')) exec('sp_rename '''+@pkfixCategory+''', ''PK_CATEGORY''')
DECLARE @pkfixCustomer varchar(500)
SET @pkfixCustomer = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Customer')
if @pkfixCustomer <> '' and (BINARY_CHECKSUM(@pkfixCustomer) <> BINARY_CHECKSUM('PK_CUSTOMER')) exec('sp_rename '''+@pkfixCustomer+''', ''PK_CUSTOMER''')
DECLARE @pkfixEmployee varchar(500)
SET @pkfixEmployee = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Employee')
if @pkfixEmployee <> '' and (BINARY_CHECKSUM(@pkfixEmployee) <> BINARY_CHECKSUM('PK_EMPLOYEE')) exec('sp_rename '''+@pkfixEmployee+''', ''PK_EMPLOYEE''')
DECLARE @pkfixEmployeeTerritory varchar(500)
SET @pkfixEmployeeTerritory = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'EmployeeTerritory')
if @pkfixEmployeeTerritory <> '' and (BINARY_CHECKSUM(@pkfixEmployeeTerritory) <> BINARY_CHECKSUM('PK_EMPLOYEETERRITORY')) exec('sp_rename '''+@pkfixEmployeeTerritory+''', ''PK_EMPLOYEETERRITORY''')
DECLARE @pkfixOrder varchar(500)
SET @pkfixOrder = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Order')
if @pkfixOrder <> '' and (BINARY_CHECKSUM(@pkfixOrder) <> BINARY_CHECKSUM('PK_ORDER')) exec('sp_rename '''+@pkfixOrder+''', ''PK_ORDER''')
DECLARE @pkfixOrderDetail varchar(500)
SET @pkfixOrderDetail = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'OrderDetail')
if @pkfixOrderDetail <> '' and (BINARY_CHECKSUM(@pkfixOrderDetail) <> BINARY_CHECKSUM('PK_ORDERDETAIL')) exec('sp_rename '''+@pkfixOrderDetail+''', ''PK_ORDERDETAIL''')
DECLARE @pkfixProduct varchar(500)
SET @pkfixProduct = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Product')
if @pkfixProduct <> '' and (BINARY_CHECKSUM(@pkfixProduct) <> BINARY_CHECKSUM('PK_PRODUCT')) exec('sp_rename '''+@pkfixProduct+''', ''PK_PRODUCT''')
DECLARE @pkfixRegion varchar(500)
SET @pkfixRegion = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Region')
if @pkfixRegion <> '' and (BINARY_CHECKSUM(@pkfixRegion) <> BINARY_CHECKSUM('PK_REGION')) exec('sp_rename '''+@pkfixRegion+''', ''PK_REGION''')
DECLARE @pkfixShipper varchar(500)
SET @pkfixShipper = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Shipper')
if @pkfixShipper <> '' and (BINARY_CHECKSUM(@pkfixShipper) <> BINARY_CHECKSUM('PK_SHIPPER')) exec('sp_rename '''+@pkfixShipper+''', ''PK_SHIPPER''')
DECLARE @pkfixSupplier varchar(500)
SET @pkfixSupplier = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Supplier')
if @pkfixSupplier <> '' and (BINARY_CHECKSUM(@pkfixSupplier) <> BINARY_CHECKSUM('PK_SUPPLIER')) exec('sp_rename '''+@pkfixSupplier+''', ''PK_SUPPLIER''')
DECLARE @pkfixSystemUser varchar(500)
SET @pkfixSystemUser = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'SystemUser')
if @pkfixSystemUser <> '' and (BINARY_CHECKSUM(@pkfixSystemUser) <> BINARY_CHECKSUM('PK_SYSTEMUSER')) exec('sp_rename '''+@pkfixSystemUser+''', ''PK_SYSTEMUSER''')
DECLARE @pkfixTerritory varchar(500)
SET @pkfixTerritory = (SELECT top 1 i.name AS IndexName FROM sys.indexes AS i WHERE i.is_primary_key = 1 AND OBJECT_NAME(i.OBJECT_ID) = 'Territory')
if @pkfixTerritory <> '' and (BINARY_CHECKSUM(@pkfixTerritory) <> BINARY_CHECKSUM('PK_TERRITORY')) exec('sp_rename '''+@pkfixTerritory+''', ''PK_TERRITORY''')
GO

--##SECTION END [RENAME PK]

--##SECTION BEGIN [DROP PK]

--##SECTION END [DROP PK]

--##SECTION BEGIN [CREATE PK]

--PRIMARY KEY FOR TABLE [Category]
if not exists(select * from sysobjects where name = 'PK_CATEGORY' and xtype = 'PK')
ALTER TABLE [dbo].[Category] WITH NOCHECK ADD 
CONSTRAINT [PK_CATEGORY] PRIMARY KEY CLUSTERED
(
	[CategoryId]
)
GO

--PRIMARY KEY FOR TABLE [Customer]
if not exists(select * from sysobjects where name = 'PK_CUSTOMER' and xtype = 'PK')
ALTER TABLE [dbo].[Customer] WITH NOCHECK ADD 
CONSTRAINT [PK_CUSTOMER] PRIMARY KEY CLUSTERED
(
	[UserId]
)
GO

--PRIMARY KEY FOR TABLE [Employee]
if not exists(select * from sysobjects where name = 'PK_EMPLOYEE' and xtype = 'PK')
ALTER TABLE [dbo].[Employee] WITH NOCHECK ADD 
CONSTRAINT [PK_EMPLOYEE] PRIMARY KEY CLUSTERED
(
	[UserId]
)
GO

--PRIMARY KEY FOR TABLE [EmployeeTerritory]
if not exists(select * from sysobjects where name = 'PK_EMPLOYEETERRITORY' and xtype = 'PK')
ALTER TABLE [dbo].[EmployeeTerritory] WITH NOCHECK ADD 
CONSTRAINT [PK_EMPLOYEETERRITORY] PRIMARY KEY CLUSTERED
(
	[TerritoryId],
	[UserId]
)
GO

--PRIMARY KEY FOR TABLE [Order]
if not exists(select * from sysobjects where name = 'PK_ORDER' and xtype = 'PK')
ALTER TABLE [dbo].[Order] WITH NOCHECK ADD 
CONSTRAINT [PK_ORDER] PRIMARY KEY CLUSTERED
(
	[OrderId]
)
GO

--PRIMARY KEY FOR TABLE [OrderDetail]
if not exists(select * from sysobjects where name = 'PK_ORDERDETAIL' and xtype = 'PK')
ALTER TABLE [dbo].[OrderDetail] WITH NOCHECK ADD 
CONSTRAINT [PK_ORDERDETAIL] PRIMARY KEY CLUSTERED
(
	[OrderDetailId]
)
GO

--PRIMARY KEY FOR TABLE [Product]
if not exists(select * from sysobjects where name = 'PK_PRODUCT' and xtype = 'PK')
ALTER TABLE [dbo].[Product] WITH NOCHECK ADD 
CONSTRAINT [PK_PRODUCT] PRIMARY KEY CLUSTERED
(
	[ProductId]
)
GO

--PRIMARY KEY FOR TABLE [Region]
if not exists(select * from sysobjects where name = 'PK_REGION' and xtype = 'PK')
ALTER TABLE [dbo].[Region] WITH NOCHECK ADD 
CONSTRAINT [PK_REGION] PRIMARY KEY CLUSTERED
(
	[RegionId]
)
GO

--PRIMARY KEY FOR TABLE [Shipper]
if not exists(select * from sysobjects where name = 'PK_SHIPPER' and xtype = 'PK')
ALTER TABLE [dbo].[Shipper] WITH NOCHECK ADD 
CONSTRAINT [PK_SHIPPER] PRIMARY KEY CLUSTERED
(
	[ShipperId]
)
GO

--PRIMARY KEY FOR TABLE [Supplier]
if not exists(select * from sysobjects where name = 'PK_SUPPLIER' and xtype = 'PK')
ALTER TABLE [dbo].[Supplier] WITH NOCHECK ADD 
CONSTRAINT [PK_SUPPLIER] PRIMARY KEY CLUSTERED
(
	[UserId]
)
GO

--PRIMARY KEY FOR TABLE [SystemUser]
if not exists(select * from sysobjects where name = 'PK_SYSTEMUSER' and xtype = 'PK')
ALTER TABLE [dbo].[SystemUser] WITH NOCHECK ADD 
CONSTRAINT [PK_SYSTEMUSER] PRIMARY KEY CLUSTERED
(
	[UserId]
)
GO

--PRIMARY KEY FOR TABLE [Territory]
if not exists(select * from sysobjects where name = 'PK_TERRITORY' and xtype = 'PK')
ALTER TABLE [dbo].[Territory] WITH NOCHECK ADD 
CONSTRAINT [PK_TERRITORY] PRIMARY KEY CLUSTERED
(
	[TerritoryId]
)
GO

--##SECTION END [CREATE PK]

--##SECTION BEGIN [AUDIT TABLES PK]

--DROP PRIMARY KEY FOR TABLE [__AUDIT__CATEGORY]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__CATEGORY'))
ALTER TABLE [dbo].[__AUDIT__CATEGORY] DROP CONSTRAINT [PK___AUDIT__CATEGORY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__CUSTOMER]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__CUSTOMER'))
ALTER TABLE [dbo].[__AUDIT__CUSTOMER] DROP CONSTRAINT [PK___AUDIT__CUSTOMER]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__EMPLOYEE]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__EMPLOYEE'))
ALTER TABLE [dbo].[__AUDIT__EMPLOYEE] DROP CONSTRAINT [PK___AUDIT__EMPLOYEE]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__EMPLOYEETERRITORY]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__EMPLOYEETERRITORY'))
ALTER TABLE [dbo].[__AUDIT__EMPLOYEETERRITORY] DROP CONSTRAINT [PK___AUDIT__EMPLOYEETERRITORY]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__ORDER]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__ORDER'))
ALTER TABLE [dbo].[__AUDIT__ORDER] DROP CONSTRAINT [PK___AUDIT__ORDER]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__ORDERDETAIL]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__ORDERDETAIL'))
ALTER TABLE [dbo].[__AUDIT__ORDERDETAIL] DROP CONSTRAINT [PK___AUDIT__ORDERDETAIL]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__PRODUCT]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__PRODUCT'))
ALTER TABLE [dbo].[__AUDIT__PRODUCT] DROP CONSTRAINT [PK___AUDIT__PRODUCT]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__REGION]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__REGION'))
ALTER TABLE [dbo].[__AUDIT__REGION] DROP CONSTRAINT [PK___AUDIT__REGION]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__SHIPPER]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__SHIPPER'))
ALTER TABLE [dbo].[__AUDIT__SHIPPER] DROP CONSTRAINT [PK___AUDIT__SHIPPER]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__SUPPLIER]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__SUPPLIER'))
ALTER TABLE [dbo].[__AUDIT__SUPPLIER] DROP CONSTRAINT [PK___AUDIT__SUPPLIER]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__SYSTEMUSER]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__SYSTEMUSER'))
ALTER TABLE [dbo].[__AUDIT__SYSTEMUSER] DROP CONSTRAINT [PK___AUDIT__SYSTEMUSER]
GO

--DROP PRIMARY KEY FOR TABLE [__AUDIT__TERRITORY]
if exists (select * from dbo.sysobjects where id = object_id(N'PK___AUDIT__TERRITORY'))
ALTER TABLE [dbo].[__AUDIT__TERRITORY] DROP CONSTRAINT [PK___AUDIT__TERRITORY]
GO

--##SECTION END [AUDIT TABLES PK]

--FOREIGN KEY RELATIONSHIP [SystemUser] -> [Customer] ([SystemUser].[UserId] -> [Customer].[UserId])
if not exists(select * from sysobjects where name = 'FK__CUSTOMER_SYSTEMUSER' and xtype = 'F')
ALTER TABLE [dbo].[Customer] ADD 
CONSTRAINT [FK__CUSTOMER_SYSTEMUSER] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[SystemUser] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [Employee] -> [Employee] ([Employee].[UserId] -> [Employee].[ReportsTo])
if not exists(select * from sysobjects where name = 'FK_REPORTSTO_EMPLOYEE_EMPLOYEE' and xtype = 'F')
ALTER TABLE [dbo].[Employee] ADD 
CONSTRAINT [FK_REPORTSTO_EMPLOYEE_EMPLOYEE] FOREIGN KEY 
(
	[ReportsTo]
) REFERENCES [dbo].[Employee] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [SystemUser] -> [Employee] ([SystemUser].[UserId] -> [Employee].[UserId])
if not exists(select * from sysobjects where name = 'FK__EMPLOYEE_SYSTEMUSER' and xtype = 'F')
ALTER TABLE [dbo].[Employee] ADD 
CONSTRAINT [FK__EMPLOYEE_SYSTEMUSER] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[SystemUser] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [Territory] -> [EmployeeTerritory] ([Territory].[TerritoryId] -> [EmployeeTerritory].[TerritoryId])
if not exists(select * from sysobjects where name = 'FK__EMPLOYEETERRITORY_TERRITORY' and xtype = 'F')
ALTER TABLE [dbo].[EmployeeTerritory] ADD 
CONSTRAINT [FK__EMPLOYEETERRITORY_TERRITORY] FOREIGN KEY 
(
	[TerritoryId]
) REFERENCES [dbo].[Territory] (
	[TerritoryId]
)
GO

--FOREIGN KEY RELATIONSHIP [Employee] -> [EmployeeTerritory] ([Employee].[UserId] -> [EmployeeTerritory].[UserId])
if not exists(select * from sysobjects where name = 'FK__EMPLOYEETERRITORY_EMPLOYEE' and xtype = 'F')
ALTER TABLE [dbo].[EmployeeTerritory] ADD 
CONSTRAINT [FK__EMPLOYEETERRITORY_EMPLOYEE] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[Employee] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [Shipper] -> [Order] ([Shipper].[ShipperId] -> [Order].[ShipperId])
if not exists(select * from sysobjects where name = 'FK__ORDER_SHIPPER' and xtype = 'F')
ALTER TABLE [dbo].[Order] ADD 
CONSTRAINT [FK__ORDER_SHIPPER] FOREIGN KEY 
(
	[ShipperId]
) REFERENCES [dbo].[Shipper] (
	[ShipperId]
)
GO

--FOREIGN KEY RELATIONSHIP [Employee] -> [Order] ([Employee].[UserId] -> [Order].[EmployeeId])
if not exists(select * from sysobjects where name = 'FK__ORDER_EMPLOYEE' and xtype = 'F')
ALTER TABLE [dbo].[Order] ADD 
CONSTRAINT [FK__ORDER_EMPLOYEE] FOREIGN KEY 
(
	[EmployeeId]
) REFERENCES [dbo].[Employee] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [Customer] -> [Order] ([Customer].[UserId] -> [Order].[UserId])
if not exists(select * from sysobjects where name = 'FK__ORDER_CUSTOMER' and xtype = 'F')
ALTER TABLE [dbo].[Order] ADD 
CONSTRAINT [FK__ORDER_CUSTOMER] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[Customer] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [Product] -> [OrderDetail] ([Product].[ProductId] -> [OrderDetail].[ProductId])
if not exists(select * from sysobjects where name = 'FK__ORDERDETAIL_PRODUCT' and xtype = 'F')
ALTER TABLE [dbo].[OrderDetail] ADD 
CONSTRAINT [FK__ORDERDETAIL_PRODUCT] FOREIGN KEY 
(
	[ProductId]
) REFERENCES [dbo].[Product] (
	[ProductId]
)
GO

--FOREIGN KEY RELATIONSHIP [Order] -> [OrderDetail] ([Order].[OrderId] -> [OrderDetail].[OrderId])
if not exists(select * from sysobjects where name = 'FK__ORDERDETAIL_ORDER' and xtype = 'F')
ALTER TABLE [dbo].[OrderDetail] ADD 
CONSTRAINT [FK__ORDERDETAIL_ORDER] FOREIGN KEY 
(
	[OrderId]
) REFERENCES [dbo].[Order] (
	[OrderId]
)
GO

--FOREIGN KEY RELATIONSHIP [Supplier] -> [Product] ([Supplier].[UserId] -> [Product].[SupplierId])
if not exists(select * from sysobjects where name = 'FK__PRODUCT_SUPPLIER' and xtype = 'F')
ALTER TABLE [dbo].[Product] ADD 
CONSTRAINT [FK__PRODUCT_SUPPLIER] FOREIGN KEY 
(
	[SupplierId]
) REFERENCES [dbo].[Supplier] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [Category] -> [Product] ([Category].[CategoryId] -> [Product].[CategoryId])
if not exists(select * from sysobjects where name = 'FK__PRODUCT_CATEGORY' and xtype = 'F')
ALTER TABLE [dbo].[Product] ADD 
CONSTRAINT [FK__PRODUCT_CATEGORY] FOREIGN KEY 
(
	[CategoryId]
) REFERENCES [dbo].[Category] (
	[CategoryId]
)
GO

--FOREIGN KEY RELATIONSHIP [SystemUser] -> [Supplier] ([SystemUser].[UserId] -> [Supplier].[UserId])
if not exists(select * from sysobjects where name = 'FK__SUPPLIER_SYSTEMUSER' and xtype = 'F')
ALTER TABLE [dbo].[Supplier] ADD 
CONSTRAINT [FK__SUPPLIER_SYSTEMUSER] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[SystemUser] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [Region] -> [Territory] ([Region].[RegionId] -> [Territory].[RegionId])
if not exists(select * from sysobjects where name = 'FK__TERRITORY_REGION' and xtype = 'F')
ALTER TABLE [dbo].[Territory] ADD 
CONSTRAINT [FK__TERRITORY_REGION] FOREIGN KEY 
(
	[RegionId]
) REFERENCES [dbo].[Region] (
	[RegionId]
)
GO

--##SECTION BEGIN [CREATE INDEXES]

--##SECTION END [CREATE INDEXES]

--##SECTION BEGIN [TENANT INDEXES]

--##SECTION END [TENANT INDEXES]

--##SECTION BEGIN [REMOVE DEFAULTS]

--BEGIN DEFAULTS FOR TABLE [Category]
--DROP CONSTRAINT FOR '[Category].[CategoryId]' if one exists
declare @Category_CategoryId varchar(500)
set @Category_CategoryId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Category' and a.name = 'CategoryId')
if (@Category_CategoryId IS NOT NULL) exec ('ALTER TABLE [Category] DROP CONSTRAINT [' + @Category_CategoryId + ']')

--DROP CONSTRAINT FOR '[Category].[Description]' if one exists
declare @Category_Description varchar(500)
set @Category_Description = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Category' and a.name = 'Description')
if (@Category_Description IS NOT NULL) exec ('ALTER TABLE [Category] DROP CONSTRAINT [' + @Category_Description + ']')

--DROP CONSTRAINT FOR '[Category].[Name]' if one exists
declare @Category_Name varchar(500)
set @Category_Name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Category' and a.name = 'Name')
if (@Category_Name IS NOT NULL) exec ('ALTER TABLE [Category] DROP CONSTRAINT [' + @Category_Name + ']')

--DROP CONSTRAINT FOR '[Category].[Picture]' if one exists
declare @Category_Picture varchar(500)
set @Category_Picture = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Category' and a.name = 'Picture')
if (@Category_Picture IS NOT NULL) exec ('ALTER TABLE [Category] DROP CONSTRAINT [' + @Category_Picture + ']')

--END DEFAULTS FOR TABLE [Category]
GO

--BEGIN DEFAULTS FOR TABLE [Customer]
--DROP CONSTRAINT FOR '[Customer].[Code]' if one exists
declare @Customer_Code varchar(500)
set @Customer_Code = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Customer' and a.name = 'Code')
if (@Customer_Code IS NOT NULL) exec ('ALTER TABLE [Customer] DROP CONSTRAINT [' + @Customer_Code + ']')

--DROP CONSTRAINT FOR '[Customer].[CompanyName]' if one exists
declare @Customer_CompanyName varchar(500)
set @Customer_CompanyName = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Customer' and a.name = 'CompanyName')
if (@Customer_CompanyName IS NOT NULL) exec ('ALTER TABLE [Customer] DROP CONSTRAINT [' + @Customer_CompanyName + ']')

--DROP CONSTRAINT FOR '[Customer].[Email]' if one exists
declare @Customer_Email varchar(500)
set @Customer_Email = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Customer' and a.name = 'Email')
if (@Customer_Email IS NOT NULL) exec ('ALTER TABLE [Customer] DROP CONSTRAINT [' + @Customer_Email + ']')

--DROP CONSTRAINT FOR '[Customer].[Title]' if one exists
declare @Customer_Title varchar(500)
set @Customer_Title = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Customer' and a.name = 'Title')
if (@Customer_Title IS NOT NULL) exec ('ALTER TABLE [Customer] DROP CONSTRAINT [' + @Customer_Title + ']')

--DROP CONSTRAINT FOR '[Customer].[UserId]' if one exists
declare @Customer_UserId varchar(500)
set @Customer_UserId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Customer' and a.name = 'UserId')
if (@Customer_UserId IS NOT NULL) exec ('ALTER TABLE [Customer] DROP CONSTRAINT [' + @Customer_UserId + ']')

--END DEFAULTS FOR TABLE [Customer]
GO

--BEGIN DEFAULTS FOR TABLE [Employee]
--DROP CONSTRAINT FOR '[Employee].[BirthDate]' if one exists
declare @Employee_BirthDate varchar(500)
set @Employee_BirthDate = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Employee' and a.name = 'BirthDate')
if (@Employee_BirthDate IS NOT NULL) exec ('ALTER TABLE [Employee] DROP CONSTRAINT [' + @Employee_BirthDate + ']')

--DROP CONSTRAINT FOR '[Employee].[HireDate]' if one exists
declare @Employee_HireDate varchar(500)
set @Employee_HireDate = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Employee' and a.name = 'HireDate')
if (@Employee_HireDate IS NOT NULL) exec ('ALTER TABLE [Employee] DROP CONSTRAINT [' + @Employee_HireDate + ']')

--DROP CONSTRAINT FOR '[Employee].[Notes]' if one exists
declare @Employee_Notes varchar(500)
set @Employee_Notes = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Employee' and a.name = 'Notes')
if (@Employee_Notes IS NOT NULL) exec ('ALTER TABLE [Employee] DROP CONSTRAINT [' + @Employee_Notes + ']')

--DROP CONSTRAINT FOR '[Employee].[PhoneExt]' if one exists
declare @Employee_PhoneExt varchar(500)
set @Employee_PhoneExt = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Employee' and a.name = 'PhoneExt')
if (@Employee_PhoneExt IS NOT NULL) exec ('ALTER TABLE [Employee] DROP CONSTRAINT [' + @Employee_PhoneExt + ']')

--DROP CONSTRAINT FOR '[Employee].[Photo]' if one exists
declare @Employee_Photo varchar(500)
set @Employee_Photo = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Employee' and a.name = 'Photo')
if (@Employee_Photo IS NOT NULL) exec ('ALTER TABLE [Employee] DROP CONSTRAINT [' + @Employee_Photo + ']')

--DROP CONSTRAINT FOR '[Employee].[PhotoPath]' if one exists
declare @Employee_PhotoPath varchar(500)
set @Employee_PhotoPath = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Employee' and a.name = 'PhotoPath')
if (@Employee_PhotoPath IS NOT NULL) exec ('ALTER TABLE [Employee] DROP CONSTRAINT [' + @Employee_PhotoPath + ']')

--DROP CONSTRAINT FOR '[Employee].[ReportsTo]' if one exists
declare @Employee_ReportsTo varchar(500)
set @Employee_ReportsTo = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Employee' and a.name = 'ReportsTo')
if (@Employee_ReportsTo IS NOT NULL) exec ('ALTER TABLE [Employee] DROP CONSTRAINT [' + @Employee_ReportsTo + ']')

--DROP CONSTRAINT FOR '[Employee].[Title]' if one exists
declare @Employee_Title varchar(500)
set @Employee_Title = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Employee' and a.name = 'Title')
if (@Employee_Title IS NOT NULL) exec ('ALTER TABLE [Employee] DROP CONSTRAINT [' + @Employee_Title + ']')

--DROP CONSTRAINT FOR '[Employee].[TitleOfCourtesy]' if one exists
declare @Employee_TitleOfCourtesy varchar(500)
set @Employee_TitleOfCourtesy = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Employee' and a.name = 'TitleOfCourtesy')
if (@Employee_TitleOfCourtesy IS NOT NULL) exec ('ALTER TABLE [Employee] DROP CONSTRAINT [' + @Employee_TitleOfCourtesy + ']')

--DROP CONSTRAINT FOR '[Employee].[UserId]' if one exists
declare @Employee_UserId varchar(500)
set @Employee_UserId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Employee' and a.name = 'UserId')
if (@Employee_UserId IS NOT NULL) exec ('ALTER TABLE [Employee] DROP CONSTRAINT [' + @Employee_UserId + ']')

--END DEFAULTS FOR TABLE [Employee]
GO

--BEGIN DEFAULTS FOR TABLE [EmployeeTerritory]
--DROP CONSTRAINT FOR '[EmployeeTerritory].[TerritoryId]' if one exists
declare @EmployeeTerritory_TerritoryId varchar(500)
set @EmployeeTerritory_TerritoryId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='EmployeeTerritory' and a.name = 'TerritoryId')
if (@EmployeeTerritory_TerritoryId IS NOT NULL) exec ('ALTER TABLE [EmployeeTerritory] DROP CONSTRAINT [' + @EmployeeTerritory_TerritoryId + ']')

--DROP CONSTRAINT FOR '[EmployeeTerritory].[UserId]' if one exists
declare @EmployeeTerritory_UserId varchar(500)
set @EmployeeTerritory_UserId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='EmployeeTerritory' and a.name = 'UserId')
if (@EmployeeTerritory_UserId IS NOT NULL) exec ('ALTER TABLE [EmployeeTerritory] DROP CONSTRAINT [' + @EmployeeTerritory_UserId + ']')

--END DEFAULTS FOR TABLE [EmployeeTerritory]
GO

--BEGIN DEFAULTS FOR TABLE [Order]
--DROP CONSTRAINT FOR '[Order].[EmployeeId]' if one exists
declare @Order_EmployeeId varchar(500)
set @Order_EmployeeId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'EmployeeId')
if (@Order_EmployeeId IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_EmployeeId + ']')

--DROP CONSTRAINT FOR '[Order].[Freight]' if one exists
declare @Order_Freight varchar(500)
set @Order_Freight = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'Freight')
if (@Order_Freight IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_Freight + ']')

--DROP CONSTRAINT FOR '[Order].[OrderDate]' if one exists
declare @Order_OrderDate varchar(500)
set @Order_OrderDate = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'OrderDate')
if (@Order_OrderDate IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_OrderDate + ']')

--DROP CONSTRAINT FOR '[Order].[OrderId]' if one exists
declare @Order_OrderId varchar(500)
set @Order_OrderId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'OrderId')
if (@Order_OrderId IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_OrderId + ']')

--DROP CONSTRAINT FOR '[Order].[RequiredDate]' if one exists
declare @Order_RequiredDate varchar(500)
set @Order_RequiredDate = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'RequiredDate')
if (@Order_RequiredDate IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_RequiredDate + ']')

--DROP CONSTRAINT FOR '[Order].[ShipAddress]' if one exists
declare @Order_ShipAddress varchar(500)
set @Order_ShipAddress = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'ShipAddress')
if (@Order_ShipAddress IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_ShipAddress + ']')

--DROP CONSTRAINT FOR '[Order].[ShipCity]' if one exists
declare @Order_ShipCity varchar(500)
set @Order_ShipCity = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'ShipCity')
if (@Order_ShipCity IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_ShipCity + ']')

--DROP CONSTRAINT FOR '[Order].[ShipCountry]' if one exists
declare @Order_ShipCountry varchar(500)
set @Order_ShipCountry = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'ShipCountry')
if (@Order_ShipCountry IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_ShipCountry + ']')

--DROP CONSTRAINT FOR '[Order].[ShipName]' if one exists
declare @Order_ShipName varchar(500)
set @Order_ShipName = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'ShipName')
if (@Order_ShipName IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_ShipName + ']')

--DROP CONSTRAINT FOR '[Order].[ShippedDate]' if one exists
declare @Order_ShippedDate varchar(500)
set @Order_ShippedDate = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'ShippedDate')
if (@Order_ShippedDate IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_ShippedDate + ']')

--DROP CONSTRAINT FOR '[Order].[ShipperId]' if one exists
declare @Order_ShipperId varchar(500)
set @Order_ShipperId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'ShipperId')
if (@Order_ShipperId IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_ShipperId + ']')

--DROP CONSTRAINT FOR '[Order].[ShipPostalCode]' if one exists
declare @Order_ShipPostalCode varchar(500)
set @Order_ShipPostalCode = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'ShipPostalCode')
if (@Order_ShipPostalCode IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_ShipPostalCode + ']')

--DROP CONSTRAINT FOR '[Order].[ShipRegion]' if one exists
declare @Order_ShipRegion varchar(500)
set @Order_ShipRegion = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'ShipRegion')
if (@Order_ShipRegion IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_ShipRegion + ']')

--DROP CONSTRAINT FOR '[Order].[UserId]' if one exists
declare @Order_UserId varchar(500)
set @Order_UserId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Order' and a.name = 'UserId')
if (@Order_UserId IS NOT NULL) exec ('ALTER TABLE [Order] DROP CONSTRAINT [' + @Order_UserId + ']')

--END DEFAULTS FOR TABLE [Order]
GO

--BEGIN DEFAULTS FOR TABLE [OrderDetail]
--DROP CONSTRAINT FOR '[OrderDetail].[Discount]' if one exists
declare @OrderDetail_Discount varchar(500)
set @OrderDetail_Discount = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='OrderDetail' and a.name = 'Discount')
if (@OrderDetail_Discount IS NOT NULL) exec ('ALTER TABLE [OrderDetail] DROP CONSTRAINT [' + @OrderDetail_Discount + ']')

--DROP CONSTRAINT FOR '[OrderDetail].[OrderDetailId]' if one exists
declare @OrderDetail_OrderDetailId varchar(500)
set @OrderDetail_OrderDetailId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='OrderDetail' and a.name = 'OrderDetailId')
if (@OrderDetail_OrderDetailId IS NOT NULL) exec ('ALTER TABLE [OrderDetail] DROP CONSTRAINT [' + @OrderDetail_OrderDetailId + ']')

--DROP CONSTRAINT FOR '[OrderDetail].[OrderId]' if one exists
declare @OrderDetail_OrderId varchar(500)
set @OrderDetail_OrderId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='OrderDetail' and a.name = 'OrderId')
if (@OrderDetail_OrderId IS NOT NULL) exec ('ALTER TABLE [OrderDetail] DROP CONSTRAINT [' + @OrderDetail_OrderId + ']')

--DROP CONSTRAINT FOR '[OrderDetail].[ProductId]' if one exists
declare @OrderDetail_ProductId varchar(500)
set @OrderDetail_ProductId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='OrderDetail' and a.name = 'ProductId')
if (@OrderDetail_ProductId IS NOT NULL) exec ('ALTER TABLE [OrderDetail] DROP CONSTRAINT [' + @OrderDetail_ProductId + ']')

--DROP CONSTRAINT FOR '[OrderDetail].[Quantity]' if one exists
declare @OrderDetail_Quantity varchar(500)
set @OrderDetail_Quantity = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='OrderDetail' and a.name = 'Quantity')
if (@OrderDetail_Quantity IS NOT NULL) exec ('ALTER TABLE [OrderDetail] DROP CONSTRAINT [' + @OrderDetail_Quantity + ']')

--DROP CONSTRAINT FOR '[OrderDetail].[UnitPrice]' if one exists
declare @OrderDetail_UnitPrice varchar(500)
set @OrderDetail_UnitPrice = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='OrderDetail' and a.name = 'UnitPrice')
if (@OrderDetail_UnitPrice IS NOT NULL) exec ('ALTER TABLE [OrderDetail] DROP CONSTRAINT [' + @OrderDetail_UnitPrice + ']')

--END DEFAULTS FOR TABLE [OrderDetail]
GO

--BEGIN DEFAULTS FOR TABLE [Product]
--DROP CONSTRAINT FOR '[Product].[CategoryId]' if one exists
declare @Product_CategoryId varchar(500)
set @Product_CategoryId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Product' and a.name = 'CategoryId')
if (@Product_CategoryId IS NOT NULL) exec ('ALTER TABLE [Product] DROP CONSTRAINT [' + @Product_CategoryId + ']')

--DROP CONSTRAINT FOR '[Product].[Discontinued]' if one exists
declare @Product_Discontinued varchar(500)
set @Product_Discontinued = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Product' and a.name = 'Discontinued')
if (@Product_Discontinued IS NOT NULL) exec ('ALTER TABLE [Product] DROP CONSTRAINT [' + @Product_Discontinued + ']')

--DROP CONSTRAINT FOR '[Product].[Name]' if one exists
declare @Product_Name varchar(500)
set @Product_Name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Product' and a.name = 'Name')
if (@Product_Name IS NOT NULL) exec ('ALTER TABLE [Product] DROP CONSTRAINT [' + @Product_Name + ']')

--DROP CONSTRAINT FOR '[Product].[ProductId]' if one exists
declare @Product_ProductId varchar(500)
set @Product_ProductId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Product' and a.name = 'ProductId')
if (@Product_ProductId IS NOT NULL) exec ('ALTER TABLE [Product] DROP CONSTRAINT [' + @Product_ProductId + ']')

--DROP CONSTRAINT FOR '[Product].[QuantityPerUnit]' if one exists
declare @Product_QuantityPerUnit varchar(500)
set @Product_QuantityPerUnit = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Product' and a.name = 'QuantityPerUnit')
if (@Product_QuantityPerUnit IS NOT NULL) exec ('ALTER TABLE [Product] DROP CONSTRAINT [' + @Product_QuantityPerUnit + ']')

--DROP CONSTRAINT FOR '[Product].[ReorderLevel]' if one exists
declare @Product_ReorderLevel varchar(500)
set @Product_ReorderLevel = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Product' and a.name = 'ReorderLevel')
if (@Product_ReorderLevel IS NOT NULL) exec ('ALTER TABLE [Product] DROP CONSTRAINT [' + @Product_ReorderLevel + ']')

--DROP CONSTRAINT FOR '[Product].[SupplierId]' if one exists
declare @Product_SupplierId varchar(500)
set @Product_SupplierId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Product' and a.name = 'SupplierId')
if (@Product_SupplierId IS NOT NULL) exec ('ALTER TABLE [Product] DROP CONSTRAINT [' + @Product_SupplierId + ']')

--DROP CONSTRAINT FOR '[Product].[UnitPrice]' if one exists
declare @Product_UnitPrice varchar(500)
set @Product_UnitPrice = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Product' and a.name = 'UnitPrice')
if (@Product_UnitPrice IS NOT NULL) exec ('ALTER TABLE [Product] DROP CONSTRAINT [' + @Product_UnitPrice + ']')

--DROP CONSTRAINT FOR '[Product].[UnitsInStock]' if one exists
declare @Product_UnitsInStock varchar(500)
set @Product_UnitsInStock = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Product' and a.name = 'UnitsInStock')
if (@Product_UnitsInStock IS NOT NULL) exec ('ALTER TABLE [Product] DROP CONSTRAINT [' + @Product_UnitsInStock + ']')

--DROP CONSTRAINT FOR '[Product].[UnitsOnOrder]' if one exists
declare @Product_UnitsOnOrder varchar(500)
set @Product_UnitsOnOrder = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Product' and a.name = 'UnitsOnOrder')
if (@Product_UnitsOnOrder IS NOT NULL) exec ('ALTER TABLE [Product] DROP CONSTRAINT [' + @Product_UnitsOnOrder + ']')

--END DEFAULTS FOR TABLE [Product]
GO

--BEGIN DEFAULTS FOR TABLE [Region]
--DROP CONSTRAINT FOR '[Region].[Name]' if one exists
declare @Region_Name varchar(500)
set @Region_Name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Region' and a.name = 'Name')
if (@Region_Name IS NOT NULL) exec ('ALTER TABLE [Region] DROP CONSTRAINT [' + @Region_Name + ']')

--DROP CONSTRAINT FOR '[Region].[RegionId]' if one exists
declare @Region_RegionId varchar(500)
set @Region_RegionId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Region' and a.name = 'RegionId')
if (@Region_RegionId IS NOT NULL) exec ('ALTER TABLE [Region] DROP CONSTRAINT [' + @Region_RegionId + ']')

--END DEFAULTS FOR TABLE [Region]
GO

--BEGIN DEFAULTS FOR TABLE [Shipper]
--DROP CONSTRAINT FOR '[Shipper].[CompanyName]' if one exists
declare @Shipper_CompanyName varchar(500)
set @Shipper_CompanyName = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Shipper' and a.name = 'CompanyName')
if (@Shipper_CompanyName IS NOT NULL) exec ('ALTER TABLE [Shipper] DROP CONSTRAINT [' + @Shipper_CompanyName + ']')

--DROP CONSTRAINT FOR '[Shipper].[Phone]' if one exists
declare @Shipper_Phone varchar(500)
set @Shipper_Phone = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Shipper' and a.name = 'Phone')
if (@Shipper_Phone IS NOT NULL) exec ('ALTER TABLE [Shipper] DROP CONSTRAINT [' + @Shipper_Phone + ']')

--DROP CONSTRAINT FOR '[Shipper].[ShipperId]' if one exists
declare @Shipper_ShipperId varchar(500)
set @Shipper_ShipperId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Shipper' and a.name = 'ShipperId')
if (@Shipper_ShipperId IS NOT NULL) exec ('ALTER TABLE [Shipper] DROP CONSTRAINT [' + @Shipper_ShipperId + ']')

--END DEFAULTS FOR TABLE [Shipper]
GO

--BEGIN DEFAULTS FOR TABLE [Supplier]
--DROP CONSTRAINT FOR '[Supplier].[CompanyName]' if one exists
declare @Supplier_CompanyName varchar(500)
set @Supplier_CompanyName = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Supplier' and a.name = 'CompanyName')
if (@Supplier_CompanyName IS NOT NULL) exec ('ALTER TABLE [Supplier] DROP CONSTRAINT [' + @Supplier_CompanyName + ']')

--DROP CONSTRAINT FOR '[Supplier].[ContactTitle]' if one exists
declare @Supplier_ContactTitle varchar(500)
set @Supplier_ContactTitle = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Supplier' and a.name = 'ContactTitle')
if (@Supplier_ContactTitle IS NOT NULL) exec ('ALTER TABLE [Supplier] DROP CONSTRAINT [' + @Supplier_ContactTitle + ']')

--DROP CONSTRAINT FOR '[Supplier].[HomePage]' if one exists
declare @Supplier_HomePage varchar(500)
set @Supplier_HomePage = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Supplier' and a.name = 'HomePage')
if (@Supplier_HomePage IS NOT NULL) exec ('ALTER TABLE [Supplier] DROP CONSTRAINT [' + @Supplier_HomePage + ']')

--DROP CONSTRAINT FOR '[Supplier].[UserId]' if one exists
declare @Supplier_UserId varchar(500)
set @Supplier_UserId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Supplier' and a.name = 'UserId')
if (@Supplier_UserId IS NOT NULL) exec ('ALTER TABLE [Supplier] DROP CONSTRAINT [' + @Supplier_UserId + ']')

--END DEFAULTS FOR TABLE [Supplier]
GO

--BEGIN DEFAULTS FOR TABLE [SystemUser]
--DROP CONSTRAINT FOR '[SystemUser].[Address]' if one exists
declare @SystemUser_Address varchar(500)
set @SystemUser_Address = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'Address')
if (@SystemUser_Address IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_Address + ']')

--DROP CONSTRAINT FOR '[SystemUser].[City]' if one exists
declare @SystemUser_City varchar(500)
set @SystemUser_City = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'City')
if (@SystemUser_City IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_City + ']')

--DROP CONSTRAINT FOR '[SystemUser].[Country]' if one exists
declare @SystemUser_Country varchar(500)
set @SystemUser_Country = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'Country')
if (@SystemUser_Country IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_Country + ']')

--DROP CONSTRAINT FOR '[SystemUser].[Fax]' if one exists
declare @SystemUser_Fax varchar(500)
set @SystemUser_Fax = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'Fax')
if (@SystemUser_Fax IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_Fax + ']')

--DROP CONSTRAINT FOR '[SystemUser].[FirstName]' if one exists
declare @SystemUser_FirstName varchar(500)
set @SystemUser_FirstName = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'FirstName')
if (@SystemUser_FirstName IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_FirstName + ']')

--DROP CONSTRAINT FOR '[SystemUser].[LastName]' if one exists
declare @SystemUser_LastName varchar(500)
set @SystemUser_LastName = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'LastName')
if (@SystemUser_LastName IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_LastName + ']')

--DROP CONSTRAINT FOR '[SystemUser].[MiddleName]' if one exists
declare @SystemUser_MiddleName varchar(500)
set @SystemUser_MiddleName = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'MiddleName')
if (@SystemUser_MiddleName IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_MiddleName + ']')

--DROP CONSTRAINT FOR '[SystemUser].[Phone]' if one exists
declare @SystemUser_Phone varchar(500)
set @SystemUser_Phone = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'Phone')
if (@SystemUser_Phone IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_Phone + ']')

--DROP CONSTRAINT FOR '[SystemUser].[PostalCode]' if one exists
declare @SystemUser_PostalCode varchar(500)
set @SystemUser_PostalCode = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'PostalCode')
if (@SystemUser_PostalCode IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_PostalCode + ']')

--DROP CONSTRAINT FOR '[SystemUser].[Region]' if one exists
declare @SystemUser_Region varchar(500)
set @SystemUser_Region = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'Region')
if (@SystemUser_Region IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_Region + ']')

--DROP CONSTRAINT FOR '[SystemUser].[UserId]' if one exists
declare @SystemUser_UserId varchar(500)
set @SystemUser_UserId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='SystemUser' and a.name = 'UserId')
if (@SystemUser_UserId IS NOT NULL) exec ('ALTER TABLE [SystemUser] DROP CONSTRAINT [' + @SystemUser_UserId + ']')

--END DEFAULTS FOR TABLE [SystemUser]
GO

--BEGIN DEFAULTS FOR TABLE [Territory]
--DROP CONSTRAINT FOR '[Territory].[Name]' if one exists
declare @Territory_Name varchar(500)
set @Territory_Name = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Territory' and a.name = 'Name')
if (@Territory_Name IS NOT NULL) exec ('ALTER TABLE [Territory] DROP CONSTRAINT [' + @Territory_Name + ']')

--DROP CONSTRAINT FOR '[Territory].[RegionId]' if one exists
declare @Territory_RegionId varchar(500)
set @Territory_RegionId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Territory' and a.name = 'RegionId')
if (@Territory_RegionId IS NOT NULL) exec ('ALTER TABLE [Territory] DROP CONSTRAINT [' + @Territory_RegionId + ']')

--DROP CONSTRAINT FOR '[Territory].[TerritoryId]' if one exists
declare @Territory_TerritoryId varchar(500)
set @Territory_TerritoryId = (select top 1 c.name from sys.all_columns a inner join sys.tables b on a.object_id = b.object_id inner join sys.default_constraints c on a.default_object_id = c.object_id where b.name='Territory' and a.name = 'TerritoryId')
if (@Territory_TerritoryId IS NOT NULL) exec ('ALTER TABLE [Territory] DROP CONSTRAINT [' + @Territory_TerritoryId + ']')

--END DEFAULTS FOR TABLE [Territory]
GO

--##SECTION END [REMOVE DEFAULTS]

--##SECTION BEGIN [CREATE DEFAULTS]

--BEGIN DEFAULTS FOR TABLE [Order]
--ADD CONSTRAINT FOR '[Order].[Freight]'
if exists(select * from sysobjects where name = 'DF__ORDER_FREIGHT' and xtype = 'D')
ALTER TABLE [dbo].[Order] DROP CONSTRAINT [DF__ORDER_FREIGHT]

if not exists(select * from sysobjects where name = 'DF__ORDER_FREIGHT' and xtype = 'D')
ALTER TABLE [dbo].[Order] ADD CONSTRAINT [DF__ORDER_FREIGHT] DEFAULT (0) FOR [Freight]

--END DEFAULTS FOR TABLE [Order]
GO

--BEGIN DEFAULTS FOR TABLE [OrderDetail]
--ADD CONSTRAINT FOR '[OrderDetail].[Discount]'
if exists(select * from sysobjects where name = 'DF__ORDERDETAIL_DISCOUNT' and xtype = 'D')
ALTER TABLE [dbo].[OrderDetail] DROP CONSTRAINT [DF__ORDERDETAIL_DISCOUNT]

if not exists(select * from sysobjects where name = 'DF__ORDERDETAIL_DISCOUNT' and xtype = 'D')
ALTER TABLE [dbo].[OrderDetail] ADD CONSTRAINT [DF__ORDERDETAIL_DISCOUNT] DEFAULT (0) FOR [Discount]

--ADD CONSTRAINT FOR '[OrderDetail].[Quantity]'
if exists(select * from sysobjects where name = 'DF__ORDERDETAIL_QUANTITY' and xtype = 'D')
ALTER TABLE [dbo].[OrderDetail] DROP CONSTRAINT [DF__ORDERDETAIL_QUANTITY]

if not exists(select * from sysobjects where name = 'DF__ORDERDETAIL_QUANTITY' and xtype = 'D')
ALTER TABLE [dbo].[OrderDetail] ADD CONSTRAINT [DF__ORDERDETAIL_QUANTITY] DEFAULT (1) FOR [Quantity]

--ADD CONSTRAINT FOR '[OrderDetail].[UnitPrice]'
if exists(select * from sysobjects where name = 'DF__ORDERDETAIL_UNITPRICE' and xtype = 'D')
ALTER TABLE [dbo].[OrderDetail] DROP CONSTRAINT [DF__ORDERDETAIL_UNITPRICE]

if not exists(select * from sysobjects where name = 'DF__ORDERDETAIL_UNITPRICE' and xtype = 'D')
ALTER TABLE [dbo].[OrderDetail] ADD CONSTRAINT [DF__ORDERDETAIL_UNITPRICE] DEFAULT (0) FOR [UnitPrice]

--END DEFAULTS FOR TABLE [OrderDetail]
GO

--BEGIN DEFAULTS FOR TABLE [Product]
--ADD CONSTRAINT FOR '[Product].[Discontinued]'
if exists(select * from sysobjects where name = 'DF__PRODUCT_DISCONTINUED' and xtype = 'D')
ALTER TABLE [dbo].[Product] DROP CONSTRAINT [DF__PRODUCT_DISCONTINUED]

if not exists(select * from sysobjects where name = 'DF__PRODUCT_DISCONTINUED' and xtype = 'D')
ALTER TABLE [dbo].[Product] ADD CONSTRAINT [DF__PRODUCT_DISCONTINUED] DEFAULT (0) FOR [Discontinued]

--ADD CONSTRAINT FOR '[Product].[ReorderLevel]'
if exists(select * from sysobjects where name = 'DF__PRODUCT_REORDERLEVEL' and xtype = 'D')
ALTER TABLE [dbo].[Product] DROP CONSTRAINT [DF__PRODUCT_REORDERLEVEL]

if not exists(select * from sysobjects where name = 'DF__PRODUCT_REORDERLEVEL' and xtype = 'D')
ALTER TABLE [dbo].[Product] ADD CONSTRAINT [DF__PRODUCT_REORDERLEVEL] DEFAULT (0) FOR [ReorderLevel]

--ADD CONSTRAINT FOR '[Product].[UnitPrice]'
if exists(select * from sysobjects where name = 'DF__PRODUCT_UNITPRICE' and xtype = 'D')
ALTER TABLE [dbo].[Product] DROP CONSTRAINT [DF__PRODUCT_UNITPRICE]

if not exists(select * from sysobjects where name = 'DF__PRODUCT_UNITPRICE' and xtype = 'D')
ALTER TABLE [dbo].[Product] ADD CONSTRAINT [DF__PRODUCT_UNITPRICE] DEFAULT (0) FOR [UnitPrice]

--ADD CONSTRAINT FOR '[Product].[UnitsInStock]'
if exists(select * from sysobjects where name = 'DF__PRODUCT_UNITSINSTOCK' and xtype = 'D')
ALTER TABLE [dbo].[Product] DROP CONSTRAINT [DF__PRODUCT_UNITSINSTOCK]

if not exists(select * from sysobjects where name = 'DF__PRODUCT_UNITSINSTOCK' and xtype = 'D')
ALTER TABLE [dbo].[Product] ADD CONSTRAINT [DF__PRODUCT_UNITSINSTOCK] DEFAULT (0) FOR [UnitsInStock]

--ADD CONSTRAINT FOR '[Product].[UnitsOnOrder]'
if exists(select * from sysobjects where name = 'DF__PRODUCT_UNITSONORDER' and xtype = 'D')
ALTER TABLE [dbo].[Product] DROP CONSTRAINT [DF__PRODUCT_UNITSONORDER]

if not exists(select * from sysobjects where name = 'DF__PRODUCT_UNITSONORDER' and xtype = 'D')
ALTER TABLE [dbo].[Product] ADD CONSTRAINT [DF__PRODUCT_UNITSONORDER] DEFAULT (0) FOR [UnitsOnOrder]

--END DEFAULTS FOR TABLE [Product]
GO

--##SECTION END [CREATE DEFAULTS]

if not exists(select * from sys.objects where [name] = '__nhydrateschema' and [type] = 'U')
BEGIN
CREATE TABLE [__nhydrateschema] (
[dbVersion] [varchar] (50) NOT NULL,
[LastUpdate] [datetime] NOT NULL,
[ModelKey] [uniqueidentifier] NOT NULL,
[History] [nvarchar](max) NOT NULL
)
if not exists(select * from sys.objects where [name] = '__pk__nhydrateschema' and [type] = 'PK')
ALTER TABLE [__nhydrateschema] WITH NOCHECK ADD CONSTRAINT [__pk__nhydrateschema] PRIMARY KEY CLUSTERED ([ModelKey])
END
GO

if not exists(select * from sys.objects where name = '__nhydrateobjects' and [type] = 'U')
CREATE TABLE [dbo].[__nhydrateobjects]
(
	[rowid] [bigint] IDENTITY(1,1) NOT NULL,
	[id] [uniqueidentifier] NULL,
	[name] [varchar](500) NOT NULL,
	[type] [varchar](10) NOT NULL,
	[schema] [varchar](500) NULL,
	[CreatedDate] [datetime] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
	[Hash] [varchar](32) NULL,
	[ModelKey] [uniqueidentifier] NOT NULL,
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_name')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_name] ON [dbo].[__nhydrateobjects]
(
	[name] ASC
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_schema')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_schema] ON [dbo].[__nhydrateobjects] 
(
	[schema] ASC
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_type')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_type] ON [dbo].[__nhydrateobjects] 
(
	[type] ASC
)

if not exists(select * from sys.indexes where name = '__ix__nhydrateobjects_modelkey')
CREATE NONCLUSTERED INDEX [__ix__nhydrateobjects_modelkey] ON [dbo].[__nhydrateobjects] 
(
	[ModelKey] ASC
)

if not exists(select * from sys.indexes where name = '__pk__nhydrateobjects')
ALTER TABLE [dbo].[__nhydrateobjects] ADD CONSTRAINT [__pk__nhydrateobjects] PRIMARY KEY CLUSTERED 
(
	[rowid] ASC
)

